import 'package:flutter/material.dart';
import 'package:flutter_sensor_backend/assets/color.dart';
import 'package:flutter_sensor_backend/assets/showPop.dart';
import 'package:flutter_sensor_backend/assets/storage.dart';
import 'package:flutter_sensor_backend/assets/svg.dart';
import 'package:flutter_sensor_backend/assets/text.dart';
import 'package:flutter_sensor_backend/component/bottom.dart';
import 'package:flutter_sensor_backend/component/niw.dart';
import 'package:flutter_sensor_backend/middleware/mid_interface.dart';
import 'package:toast/toast.dart';

class SensorStart extends StatefulWidget {
  const SensorStart({super.key});

  @override
  State<SensorStart> createState() => _SensorStartState();
}

class _SensorStartState extends State<SensorStart> {
  bool isAdmin = true;
  bool isLogin = true;

  isLogined() async {
    var t = await getStorage(key: "token", initData: "");
    if (t == "") {
      setState(() {
        isLogin = false;
      });
      // 没有Token
    } else {
      // Navigator.pushNamed(context, "/home");
    }
  }

  @override
  void initState() {
    isLogined();
    ToastContext().init(context);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: Center(
              child: ListView(
                shrinkWrap: true,
                children: [
                  os_svg(
                    width: 280,
                    height: 200,
                    path: "images/start.svg",
                  ),
                  Container(height: 35),
                  TextView(
                    "欢迎使用智能监测终端",
                    isBold: true,
                    fontSize: 24,
                    isCenter: true,
                  ),
                  Container(height: 10),
                  TextView(
                    isLogin ? "由中科大软件学院-工程实践小组设计&制作" : "请登录后使用",
                    fontSize: 14,
                    textColor: const Color(0xff838383),
                    isCenter: true,
                  ),
                  Container(height: 100),
                  Container(
                    padding: const EdgeInsets.symmetric(horizontal: 45),
                    width: MediaQuery.of(context).size.width,
                    child: TextView(
                      isLogin ? "开始使用" : (isAdmin ? "管理员登录" : "用户登录"),
                      onClick: () {
                        if (isLogin) {
                          Navigator.of(context).pushNamed("/home");
                        } else {
                          showPop(
                            context,
                            [
                              LoginSection(isAdmin: isAdmin),
                            ],
                          );
                        }
                        // Navigator.of(context).pushNamed("/home");
                      },
                      fontSize: 16,
                      backgroundColor: os_color,
                      cornerRadius: 100,
                      textColor: os_white,
                      paddingTop: 13.5,
                      paddingBottom: 13.5,
                      isCenter: true,
                      isBold: true,
                    ),
                  ),
                  Container(height: 10),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: isLogin
                        ? [
                            TextView(
                              "退出登录",
                              fontSize: 14,
                              textColor: Color(0xff6D7793),
                              isCenter: true,
                              isBold: true,
                              padding: 10,
                              onClick: () {
                                setStorage(key: "token", value: "");
                                isLogin = false;
                                setState(() {});
                              },
                            ),
                          ]
                        : [
                            TextView(
                              isAdmin ? "用户登录" : "管理员登录",
                              fontSize: 14,
                              textColor: Color(0xff6D7793),
                              isCenter: true,
                              isBold: true,
                              padding: 10,
                              onClick: () {
                                setState(() {
                                  isAdmin = !isAdmin;
                                });
                              },
                            ),
                            TextView(
                              "｜",
                              fontSize: 14,
                              textColor: Color(0xffBCBCBC),
                              isCenter: true,
                            ),
                            TextView(
                              "用户注册",
                              fontSize: 14,
                              textColor: Color(0xff6D7793),
                              isCenter: true,
                              isBold: true,
                              padding: 10,
                              onClick: () {
                                print("用户注册");
                              },
                            ),
                          ],
                  ),
                ],
              ),
            ),
          ),
          Container(
            height: 80,
            child: Bottom(),
          )
        ],
      ),
    );
  }
}

class LoginSection extends StatefulWidget {
  bool isAdmin;
  LoginSection({
    super.key,
    required this.isAdmin,
  });

  @override
  State<LoginSection> createState() => _LoginSectionState();
}

class _LoginSectionState extends State<LoginSection> {
  bool showPassword = false;
  TextEditingController _usernameEditingController =
      new TextEditingController();
  TextEditingController _passwordEditingController =
      new TextEditingController();

  login() async {
    var res = await Api().Login(
      _usernameEditingController.text,
      _passwordEditingController.text,
    );
    Toast.show(res["msg"], gravity: Toast.center);
    if (res["code"] == 0) {
      // 登录失败
    } else {
      // 登录成功
      await setStorage(key: "token", value: res["data"]);
      Navigator.pop(context);
      Navigator.pushNamed(context, "/home");
    }
  }

  initInput() {
    _usernameEditingController.text = "xusun000";
    _passwordEditingController.text = "1234567xs";
    setState(() {});
  }

  @override
  void initState() {
    initInput();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(height: 30),
        Text(
          (widget.isAdmin ? "管理员" : "用户") + "登录",
          style: TextStyle(
            color: os_black,
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
        ),
        Container(height: 25),
        ClipRRect(
          borderRadius: BorderRadius.circular(15),
          child: TextField(
            controller: _usernameEditingController,
            decoration: InputDecoration(
              fillColor: os_back,
              filled: true,
              border: InputBorder.none,
              hintText: "请输入用户名",
              contentPadding: const EdgeInsets.symmetric(
                vertical: 17.5,
                horizontal: 15,
              ),
            ),
          ),
        ),
        Container(height: 10),
        ClipRRect(
          borderRadius: BorderRadius.circular(15),
          child: TextField(
            controller: _passwordEditingController,
            obscureText: !showPassword,
            decoration: InputDecoration(
              fillColor: os_back,
              filled: true,
              border: InputBorder.none,
              hintText: "请输入密码",
              contentPadding: const EdgeInsets.symmetric(
                vertical: 17.5,
                horizontal: 15,
              ),
              suffixIcon: Container(
                padding: const EdgeInsets.only(right: 5),
                child: IconButton(
                  icon: Icon(
                    showPassword ? Icons.visibility_off : Icons.visibility,
                    size: 20,
                    color: os_deep_gray,
                  ),
                  onPressed: () {
                    setState(() {
                      showPassword = !showPassword;
                    });
                  },
                ),
              ),
            ),
          ),
        ),
        Container(height: 20),
        myInkWell(
          tap: () {
            login();
          },
          color: os_color,
          widget: Container(
            padding: EdgeInsets.symmetric(vertical: 15),
            width: MediaQuery.of(context).size.width,
            decoration: BoxDecoration(
                // color: os_color,
                ),
            child: Center(
              child: Text(
                "登录",
                style: TextStyle(
                  color: os_white,
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
          radius: 100,
        ),
      ],
    );
  }
}
